package com.ving.test.mybatis.session;

import com.ving.test.mybatis.binding.MapperMethod;
import com.ving.test.mybatis.binding.MapperProxy;
import com.ving.test.mybatis.executor.Executor;

import java.lang.reflect.Proxy;
import java.util.List;

/**
 * configuration--->sqlsession--->executor
 * 加载配置文件----》组装怎样执行----》执行
 * Created by liuweijie on 2019-11-14.
 */
public class DefaultqlSeesion implements SqlSession{

    private Configuration configuration;
    private Executor executor;

    public DefaultqlSeesion() {
    }

    public DefaultqlSeesion(Configuration configuration, Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }

    @Override
    public  <T> T selectOne(MapperMethod mapperMethod, Object statement) {

        try {
           return  executor.query(mapperMethod,statement);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public <T>T getMapper(Class<T> zlass) {

        // 核心，使用jdk动态代理实现
        return (T)Proxy.newProxyInstance(zlass.getClassLoader(),new Class[]{zlass},new MapperProxy<>(this,zlass));

    }

    public Configuration getConfiguration() {

        return configuration;
    }

    public Executor getExecutor() {
        return executor;
    }
}
